/**
 * 
 */
package com.ideazd.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ideazd.api.model.NewDetails;
import com.ideazd.api.service.NewsDetailsService;
import com.ideazd.api.tools.SysUtil;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

/**
 * @author yzz
 * @create 2016年3月24日 上午9:47:15
 */
@ControllerBind(controllerKey="news")
public class NewDetailsController extends Controller {
	private static final Logger log = Logger.getLogger(NewDetailsController.class);
	private NewsDetailsService newsDetailsService = new NewsDetailsService();
	
	/**
	 * 资讯列表
	 */
	public void getNewsList(){
		String pageIndex = getPara("pageIndex");
		String displayOrder = getPara("displayOrder");
		String detId = getPara("detId");
		Map<Object, Object> ret = new HashMap<Object,Object>();
		
		if(StrKit.isBlank(pageIndex)||StrKit.isBlank(displayOrder)){
			ret.put("code","-2");
			ret.put("msg","必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		
		try {
			
			Page<NewDetails> newDetails = newsDetailsService.newDetailsList(pageIndex,displayOrder,detId);
			List<Map<Object, Object>> newList = new ArrayList<Map<Object, Object>>();
			
			for(NewDetails newDetail : newDetails.getList()){
				Map<Object, Object> news = new HashMap<Object,Object>();
				news.put("detId", newDetail.get("detid"));
				news.put("detType", newDetail.get("dettype"));
				news.put("createTime", newDetail.get("createtime").toString().substring(0, 10));
				news.put("detTitle", newDetail.get("dettitle"));
				news.put("detSimpleTitle", newDetail.get("dettitle"));
				news.put("detPicture", PropKit.get("server_uri") +  newDetail.get("detpicture"));
				newList.add(news);
			}
			
			Map<Object, Object> data = new HashMap<Object,Object>();
			data.put("totalPage", newDetails.getTotalPage());
			data.put("newDetailsList", newList);
			
			ret.put("code","0");
			ret.put("msg","成功");
			ret.put("data", data);
			
		} catch (Exception e) {
			ret.put("code","-1");
			ret.put("msg",e.getMessage());
		}
		
		renderJson(ret);
		return;
	}
	
	/**
	 * 资讯详情
	 */
	public void getNewsDetail(){
		String detId = getPara("detId");
		Map<Object, Object> ret = new HashMap<Object,Object>();
		
		if(StrKit.isBlank(detId)){
			ret.put("code","-2");
			ret.put("msg","必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		
		
		try {
			NewDetails newDetails = NewDetails.dao.findById(detId);
			Map<Object, Object> data = new HashMap<Object,Object>();
			Map<Object, Object> news = new HashMap<Object,Object>();
			news.put("detId", newDetails.get("detid"));
			
			news.put("detContent",SysUtil.Html2Text(newDetails.get("detcontent").toString()));
			news.put("topSource", newDetails.get("detcontenturl"));
			news.put("detTitle", newDetails.get("dettitle"));
			news.put("createTime", newDetails.get("createtime"));
			news.put("readAmount", newDetails.get("readamount"));
			news.put("shareImage", PropKit.get("server_uri") +  newDetails.get("detpicture"));
			news.put("shareUrl", newDetails.get("detid"));
			data.put("newsContent", news);
			
			Page<NewDetails> newDetailsList = newsDetailsService.newDetailsList("1","2",detId);
			List<Map<Object, Object>> newList = new ArrayList<Map<Object, Object>>();
			//Map<Object, Object> newsList = new HashMap<Object,Object>();
			
			for(NewDetails newDetail : newDetailsList.getList()){
				Map<Object, Object> newDetailsDate = new HashMap<Object,Object>();
				newDetailsDate.put("detId", newDetail.get("detid"));
				newDetailsDate.put("detType", newDetail.get("dettype"));
				newDetailsDate.put("detTitle", newDetail.get("dettitle"));
				newDetailsDate.put("detSimpleTitle", newDetail.get("dettitle"));
				newDetailsDate.put("detPicture", PropKit.get("server_uri") +  newDetail.get("detpicture"));
				newList.add(newDetailsDate);
			}
			
			data.put("newDetailsList", newList);
			
			
			
			
			ret.put("data", data);
			ret.put("code","0");
			ret.put("msg","成功");
			Db.update("update sta_new_details set readamount = readamount+1 where detId='"+detId+"'");
			
		} catch (Exception e) {
			ret.put("code","-1");
			ret.put("msg",e.getMessage());
		}
		
		renderJson(ret);
		return;
	}

}
